package miejuli;


import utils.HttpUtil;
import utils.JsonUtil;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/25.
 */
public class MeiJuLiApi {

    String token="oF0JgXeWpQdTRxWLBItqdLip26iCL7D27D1997157";
    String phone="";
    String code="";
    String tokenId="";
//
//    public void login() throws IOException, URISyntaxException {
//        String url="http://api.shjmpt.com:9002/pubApi/uLogin";
//        Map<String,String> param=new HashMap<>();
//        param.put("uName","254992364");
//        param.put("pWord","123456");
//        param.put("Developer","n%2fndT29jxLQEEBHBLT4yDg%3d%3d");
//        token=getResponseString(HttpUtil.getInstance().doGet(url,param)).split("&")[0];
//        System.out.println(token);
//    }

    public void getPhone() throws IOException, URISyntaxException {
        String url="http://api.shjmpt.com:9002/pubApi/GetPhone";
        Map<String,String> param=new HashMap<>();
        param.put("ItemId","123037");
        param.put("token",token);
        param.put("Count","1");
        //param.put("Area","%E4%B8%8D%E9%99%90");
        param.put("PhoneType","0");
        param.put("Phone","");
        param.put("onlyKey","");
        phone=getResponseString(HttpUtil.getInstance().doGet(url,param)).substring(0,11);
        System.out.println("获取到手机号："+phone);
    }

    public void sendMesasge() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/getSmsCode/getSms";
        Map<String,String> param=new HashMap<>();
        param.put("phone",phone);
        param.put("smsType","1");
        System.out.println(getResponseString(HttpUtil.getInstance().doGet(url,param))+"==="+phone+"===注册短信发送");
    }

    public int getMessage() throws IOException, URISyntaxException, InterruptedException {
        code="";
        String codetemp="";
        for(int i=0;i<10;i++){
            String url="http://api.shjmpt.com:9002/pubApi/GetMessage";
            Map<String,String> param=new HashMap<>();
            param.put("token",token);
            param.put("PhoneList",phone+"-123037;");
            codetemp=getResponseString(HttpUtil.getInstance().doGet(url,param));
            //System.out.println(codetemp);
            if(codetemp.indexOf("验证码")!=-1){
                code=codetemp.substring(31,37);
                break;
            }
            Thread.sleep(6100);
        }
        if(code.length()>0){
            return 0;
        }else{
            return 1;
        }
    }

    public void regist() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/regist/userRegist";
        Map<String,String> param=new HashMap<>();
        param.put("code",code);
        param.put("password","112768b271a4c9d8dc3228d155d5134382b40b1d6cb7045a7d986b1511724ad9");
        param.put("phone",phone);
        param.put("recommendPhone","");
        param.put("typeC","");
        param.put("userType","1");
        System.out.println(getResponseString(HttpUtil.getInstance().doGet(url,param))+"==="+phone+"===注册==="+code);
    }

    public void loginYangmao() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/user/login";
        Map<String,String> param=new HashMap<>();
        param.put("password","112768b271a4c9d8dc3228d155d5134382b40b1d6cb7045a7d986b1511724ad9");
        param.put("phone",phone);
        String rep=getResponseString(HttpUtil.getInstance().doGet(url,param));
        JSONObject json= JsonUtil.toJSONObject(rep);
        tokenId=json.getJSONObject("data").getString("tokenID");
        System.out.println(rep+"==="+phone+"===登陆");
    }

    public void getSms() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/getSmsCode/getSms";
        Map<String,String> param=new HashMap<>();
        param.put("smsType","3");
        param.put("phone",phone);
        param.put("typeC","2");
        System.out.println(getResponseString(HttpUtil.getInstance().doGet(url,tokenId,param))+"==="+phone+"===发送支付验证码");
    }

    public void userPayPwd() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/user/aboutpayment/saveOrUpdateUserPayPwd";
        Map<String,String> param=new HashMap<>();
        param.put("paymentPassword","8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
        param.put("phone",phone);
        param.put("code",code);
        System.out.println(getResponseString(HttpUtil.getInstance().doGet(url,tokenId,param))+"==="+phone+"===更改支付密码");
    }

    public void releasePhone() throws IOException, URISyntaxException {
        String url="http://api.shjmpt.com:9002/pubApi/ReleasePhone";
        Map<String,String> param=new HashMap<>();
        param.put("token",token);
        param.put("phoneList",phone+"-123037;");
        getResponseString(HttpUtil.getInstance().doGet(url,param));
    }

    public void exit() throws IOException, URISyntaxException {
        String url="http://api.shjmpt.com:9002/pubApi/uExit";
        Map<String,String> param=new HashMap<>();
        param.put("token",token);
        System.out.println(getResponseString(HttpUtil.getInstance().doGet(url,param)));
    }

    public void writeFile() throws IOException {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\号池.txt",true);//true表示在文件末尾追加
        fos.write((phone+"\r\n").getBytes());
        fos.close();//流要及时关闭
    }
    public void printPhone(){
        System.out.println(phone);
    }

    public String getResponseString(HttpResponse response) throws IOException {
        return  EntityUtils.toString(response.getEntity());
    }

}
